﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace skvote
{
    public partial class admin_title : System.Web.UI.Page
    {
        private OleDbConnection connection = new conn().CreateConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            function.check();
            if (!IsPostBack)
            {
                if (Request["act"] != null && Request["act"] == "del")
                {
                    if (Request["id"] != null)
                    {
                        bool del = false;
                        connection.Close();
                        connection.Open();
                        OleDbTransaction tran = connection.BeginTransaction();
                        try
                        {                           
                            OleDbCommand cmd = new OleDbCommand();
                            cmd.Connection = connection;
                            cmd.Transaction = tran;
                            cmd.CommandText = "delete from xh_title where id=" + Request["id"].Trim();
                            cmd.ExecuteNonQuery();
                            cmd.CommandText = "delete from xh_question where tid=" + Request["id"].Trim();
                            cmd.ExecuteNonQuery();
                            tran.Commit();
                            del = true;
                        }
                        catch
                        {
                            tran.Rollback();
                        }
                        finally
                        {
                            connection.Close();
                        }
                        if (del)
                        {
                            function.successMsg("记录成功删除!");
                        }
                    }
                }
                if (!string.IsNullOrEmpty(Request["sid"]) && !string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_title where sid=" + Request["sid"].Trim() + " and title like \"%" + Request["k"].Trim() + "%\"");
                }
                else if (string.IsNullOrEmpty(Request["sid"]) && !string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_title where title like \"%" + Request["k"] + "%\"");
                }
                else if (!string.IsNullOrEmpty(Request["sid"]) && string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_title where sid=" + Request["sid"].Trim());
                }
                else
                {
                    BindData("select * from xh_title");
                }
            }
            else
            {                
            }
        }
        private void BindData(string sql)
        {
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
                DataSet ds = new DataSet();
                da.Fill(ds,"binddata");
                table2.DataSource = ds.Tables[0].DefaultView;
                table2.DataBind();
                ShowStatus(ds.Tables[0].Rows.Count);
            }
            finally
            {
                connection.Close();
            }            
        }
        private void ShowStatus(int cnt)
        {
            recordlbl.Text = cnt.ToString();
            pageslbl.Text = table2.PageCount.ToString();
            pagelbl.Text = (table2.CurrentPageIndex).ToString();
        }
        public string GetSubject(string id)
        {
            string subject = "不存在这样的主题";
            try
            {
                connection.Close();
                connection.Open();
                OleDbCommand cmd = new OleDbCommand("select * from xh_subject where id=" + id.Trim(), connection);
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    subject = "<a href='admin_title.aspx?sid=" + dr["id"].ToString() + "'>" + dr["subject"].ToString() + "</a>";
                }
                dr.Close();
                cmd.Dispose();
            }
            finally
            {
                connection.Close();
            }
            return subject;
        }
        public string GetMs(string ms)
        {
            if (ms != null && ms == "1")
            {
                return "单选";
            }
            else if (ms != null && ms == "2")
            {
                return "多选";
            }
            else
            {
                return "";
            }
        }

        protected void table2_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox headerchk = (CheckBox)e.Item.FindControl("headerchk");
                headerchk.CheckedChanged += new EventHandler(check_CheckedChange);
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                CheckBox footerchk = (CheckBox)e.Item.FindControl("footerchk");
                footerchk.CheckedChanged += new EventHandler(check_CheckedChange);
            }
            else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataGridItem item = e.Item;
                item.Attributes.Add("className","out_TableTitle3");
                item.Attributes.Add("onmouseout", "this.className='out_TableTitle3'");
                item.Attributes.Add("onmouseover", "this.className='over_TableTitle3'");
            }
        }
        private void check_CheckedChange(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            for (int i = 0; i < table2.Items.Count; i++)
            {
                DataGridItem row = table2.Items[i];
                CheckBox ele = (CheckBox)row.FindControl("choosechk");
                ele.Checked = chk.Checked;
            }
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            string ids = "";
            for (int i = 0; i < table2.Items.Count;i++)
            {
                DataGridItem item = table2.Items[i];
                CheckBox chk = (CheckBox)item.FindControl("choosechk");
                if (chk.Checked == true)
                {
                    ids += chk.ToolTip.Trim() + ",";
                }
            }
            if (string.IsNullOrEmpty(ids.Trim()))
            {
                function.showErr("没有选中要删除的标题",Response,Request);
            }
            else
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            connection.Close();
            connection.Open();
            OleDbTransaction tran = connection.BeginTransaction();
            try
            {                
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = connection;
                cmd.Transaction = tran;
                cmd.CommandText = "delete from xh_title where id in (" + ids.Trim() + ")";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from xh_question where tid in(" + ids.Trim() + ")";
                cmd.ExecuteNonQuery();
                tran.Commit();
            }
            catch
            {
                tran.Rollback();
            }
            finally
            {
                connection.Close();
            }
            Response.Redirect("admin_title.aspx?sid=" + Request["sid"] + "&k" + Request["k"]);
        }

        protected void search_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin_title.aspx?k=" + Request["k"]+"&sid="+Request["sid"]);
        }

        protected void first_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            switch (btn.CommandArgument)
            {
                case "FIRST": table2.CurrentPageIndex = 0; break;
                case "NEXT": if (table2.CurrentPageIndex < (table2.PageCount-1)) table2.CurrentPageIndex++; break;
                case "PREV": if (table2.CurrentPageIndex > 0) table2.CurrentPageIndex--; break;
                case "LAST": table2.CurrentPageIndex = table2.PageCount - 1; break;              
            }
            if (Request["sid"] != null && Request["k"] != null)
            {
                BindData("select * from xh_title where sid=" + Request["sid"].Trim() + " and title like \"%" + Request["k"].Trim() + "%\"");
            }
            else if (Request["sid"] == null && Request["k"] != null)
            {
                BindData("select * from xh_title where title like \"%" + Request["k"] + "%\"");
            }
            else if (Request["sid"] != null && Request["k"] == null)
            {
                BindData("select * from xh_title where sid=" + Request["sid"].Trim());
            }
            else
            {
                BindData("select * from xh_title");
            }
        }

        protected void table2_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            table2.CurrentPageIndex = e.NewPageIndex;
            if (Request["sid"] != null && Request["k"] != null)
            {
                BindData("select * from xh_title where sid=" + Request["sid"].Trim() + " and title like \"%" + Request["k"].Trim() + "%\"");
            }
            else if (Request["sid"] == null && Request["k"] != null)
            {
                BindData("select * from xh_title where title like \"%" + Request["k"] + "%\"");
            }
            else if (Request["sid"] != null && Request["k"] == null)
            {
                BindData("select * from xh_title where sid=" + Request["sid"].Trim());
            }
            else
            {
                BindData("select * from xh_title");
            }
        }        
    }
}
